import http from './HTTP'

/**
 * Created by hao.cheng on 2017/4/28.
 */
// 获取url的参数
export const queryString = () => {
    let _queryString = {};
    const _query = window.location.search.substr(1);
    const _vars = _query.split('&');
    _vars.forEach((v, i) => {
        const _pair = v.split('=');
        if (!_queryString.hasOwnProperty(_pair[0])) {
            _queryString[_pair[0]] = decodeURIComponent(_pair[1]);
        } else if (typeof _queryString[_pair[0]] === 'string') {
            const _arr = [ _queryString[_pair[0]], decodeURIComponent(_pair[1])];
            _queryString[_pair[0]] = _arr;
        } else {
            _queryString[_pair[0]].push(decodeURIComponent(_pair[1]));
        }
    });
    return _queryString;
};

export const setState = (state) => {
    if(state == undefined) {
        localStorage.setItem("globalState", undefined)
    }
    localStorage.setItem("globalState", JSON.stringify(state))
}

export const getState = () => {
    if(localStorage.getItem("globalState") == undefined) {
        return undefined;
    } else {
        return JSON.parse(localStorage.getItem("globalState"))
    }
}

export const setUserInfo = (state) => {
    if(state == undefined) {
        localStorage.setItem('userInfo', undefined)
    } else {
        localStorage.setItem('userInfo', JSON.stringify(state))
    }
}

export const getUserInfo = () => {
    let resp = localStorage.getItem('userInfo')
    if(resp != undefined) {
        return JSON.parse(resp)
    } else {
        return resp
    }
}

export const getDomain = () => {
    let userInfo = getUserInfo() || {}
    return userInfo['appDomain']
}
export const getOrganNo = () => {
    let userInfo = getUserInfo() || {}
    return userInfo['organNo']
}

export const getArchId = () => {
    let userInfo = getUserInfo() || {}
    return userInfo['archId']
}

export const getUserName = () => {
    let userInfo = getUserInfo() || {}
    return userInfo['userName']
}

export const HTTP = http
